package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMiguVideoConfigDao;
import org.jsbd.boss.domian.MiguVideoConfigBean;
import org.jsbd.boss.domian.MiguVideoProvinceConfig;
import org.jsbd.boss.service.IMiguVideoConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.TimeModelUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("miguVideoConfigService")
public class MiguVideoConfigServiceImpl implements IMiguVideoConfigService {
	
	@Resource
	private IMiguVideoConfigDao miguVideoConfigDao;
	@Resource
	private RedisClient<String,Object> redisClientMigu;

	@Override
	public Map<String, Object> getMiguVideoConfigListPage(DataGridModel page, MiguVideoConfigBean miguVideoConfig) {
		Map<String,Object> results = miguVideoConfigDao.getMiguMusicConfigListPage(page, miguVideoConfig);
		List<MiguVideoConfigBean> configList = (List<MiguVideoConfigBean>)results.get("rows");
		if(configList == null || configList.isEmpty()){
			return results;
		}
		
		Map<Object,Object> successMap=redisClientMigu.getHash(CacheKey.MIGUVIDEO_SUCCESS_NUM_KEY);
		Map<Object,Object> doNumMap=redisClientMigu.getHash(CacheKey.MIGUVIDEO_NEW_DO_NUM_HASH_KEY);
		
		String sucessPrefixKey=CacheKey.MIGUVIDEO_SUCCESS_NUM_KEY;
		String doPrefixKey=CacheKey.MIGUVIDEO_NEW_TASK_NUM_DONE;
		
		String doDay=DateUtil.getCurrentDateStr();
		
		for(MiguVideoConfigBean config : configList){
			String suffixKey=doPrefixKey+config.getChannelId()+"_"+config.getVersionCode()+"_"+doDay;
			Long userNum=(Long) doNumMap.get(suffixKey+"_user");
			if(userNum==null){
				userNum=0L;
			}
			Long noUserNum=(Long)doNumMap.get(suffixKey+"_nouser");
			if(noUserNum==null){
				noUserNum=0L;
			}
			long doNum=userNum+noUserNum;
			String successKey=sucessPrefixKey+","+config.getChannelId()+","+config.getVersionCode()+","+doDay;
			String successNum=(String)successMap.get(successKey);
			if(StringUtils.isBlank(successNum)){
				successNum="0,0";
			}
			
			String[] doSuccArr=successNum.split(",");
			if(!"0".equals(doSuccArr[0])) {
				config.setDoUserRate(Integer.valueOf(doSuccArr[1])*1.0/Integer.valueOf(doSuccArr[0]));
			}else {
				config.setDoUserRate(0D);
			}
			
			Integer totalNum=config.getTotalNum();
			if(totalNum==null){
				totalNum=0;
			}
			int planNum=TimeModelUtils.genMinMaxNumByTime(Integer.valueOf(totalNum+""), new Date());
			
			String totalNumStr=planNum+"|"+doNum+"|"+successNum;
			config.setTotalNumStr(totalNumStr);
		}
		
		return results;
	
	}

	@Override
	public MiguVideoConfigBean getMiguVideoConfig(MiguVideoConfigBean miguVideoConfig) {
		return miguVideoConfigDao.selectMiguVideoConfig(miguVideoConfig);
	}

	@Override
	public void add(MiguVideoConfigBean miguVideoConfig) {
		miguVideoConfigDao.insert(miguVideoConfig);
	}

	@Override
	public void update(MiguVideoConfigBean miguVideoConfig) {
		miguVideoConfigDao.update(miguVideoConfig);
	}

	@Override
	public void delete(MiguVideoConfigBean miguVideoConfig) {
		miguVideoConfigDao.delete(miguVideoConfig);
	}

	@Override
	public void updateMiguVideoStatus(MiguVideoConfigBean miguVideoConfig) {
		miguVideoConfigDao.updateMiguVideoStatus(miguVideoConfig);
	}

	@Override
	public MiguVideoConfigBean getMiguVideoConfigAll(MiguVideoConfigBean queryMvc) {
		return miguVideoConfigDao.selectMiguVideoConfigAll(queryMvc);
	}

	@Override
	public MiguVideoConfigBean getMiguVideoConfigExName(String name, Integer id) {
		return miguVideoConfigDao.selectMiguVideoConfigExName(name, id);
	}

	@Override
	public MiguVideoConfigBean getMiguVideoConfigByName(MiguVideoConfigBean queryMvc) {
		return miguVideoConfigDao.selectMiguVideoConfigByName(queryMvc);
	}

	@Override
	public MiguVideoConfigBean getMiguVideoConfigByChannelId(String channelId) {
		return miguVideoConfigDao.selectMiguVideoConfigByChannelId(channelId);
	}

	@Override
	public List<MiguVideoProvinceConfig> getProvinceById(Integer id) {
		return miguVideoConfigDao.selectProvinceById(id);
	}

	@Override
	@Transactional("transactionManagerCrack")
	public void updateProvinceById(String id, String openProvince) {
		String[] provinceIds = openProvince.split(",");
		List<String> list = new ArrayList<String>();
		List<MiguVideoProvinceConfig> mvpcs = new ArrayList<MiguVideoProvinceConfig>();
		for (String provinceId : provinceIds) {
			list.add(provinceId);
			MiguVideoProvinceConfig mvpc = new MiguVideoProvinceConfig();
			mvpc.setMiguVideoId(Integer.valueOf(id));
			mvpc.setProvince(Integer.valueOf(provinceId));
			mvpcs.add(mvpc);
		}
		miguVideoConfigDao.batchInsertProvinceConfig(mvpcs);
		
		// 删除没有选择的
		MiguVideoProvinceConfig vo = new MiguVideoProvinceConfig();
		vo.setMiguVideoId(Integer.valueOf(id));
		vo.setProvinceIds(list);
		miguVideoConfigDao.deleteProvinceConfigs(vo);
	}

	@Override
	public List<MiguVideoConfigBean> getMiguVideoConfigList(MiguVideoConfigBean bean) {
		return miguVideoConfigDao.getMiguVideoConfigList(bean);
	}

}
